package com.imooc.api.service;

import com.github.pagehelper.PageInfo;
import com.imooc.pojo.AppUser;
import com.imooc.utils.PagedGridResult;
import com.imooc.utils.RedisOperator;
import com.mongodb.client.gridfs.GridFSBucket;
import org.apache.commons.lang3.StringUtils;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @auther Levi
 * @Date: 2021/03/21/22:20
 * @Description:
 */
public class BaseService {
    @Autowired
    public RedisOperator redis;
    @Autowired
    public Sid sid;

    public static final String REDIS_ALL_CATEGORY = "redis_all_category";
    public static final Integer COMMON_PAGE = 1;
    public static final Integer COMMON_PAGESIZE = 5;
    public static final String REDIS_WRITER_FANS_COUNTS = "writer_fans_counts";
    public static final String REDIS_MY_FOLLOW_COUNTS = "my_follow_counts";
    public static final String REDIS_ARTICLE_READ_COUNTS = "redis_article_read_counts";
    public static final String REDIS_ALREADY_READ = "redis_already_read";
    public static final String REDIS_ARTICLE_COMMNET_COUNTS = "redis_article_commnet_counts";
    public static final String[] REGIONS = {"北京", "天津", "上海", "重庆",
            "河北", "山西", "辽宁", "吉林", "黑龙江", "江苏", "浙江", "安徽", "福建", "江西", "山东",
            "河南", "湖北", "湖南", "广东", "海南", "四川", "贵州", "云南", "陕西", "甘肃", "青海", "台湾",
            "内蒙古", "广西", "西藏", "宁夏", "新疆",
            "香港", "澳门"};

    public PagedGridResult pagedResultSettings(List<?> pageList, Integer page) {
        PageInfo<?> pageInfo = new PageInfo<>(pageList);
        PagedGridResult pagedResult = new PagedGridResult();
        pagedResult.setRows(pageList);
        //设置显示第几页的数据
        pagedResult.setPage(page);
        //设置总记录数
        pagedResult.setTotal(pageInfo.getPages());
        //设置总页数
        pagedResult.setRecords(pageInfo.getTotal());
        return pagedResult;
    }

    /**
     * 根据key获取redis中的数据
     * @param key
     * @return
     */
    public String getCountsFromRedis(String key) {
        String counts = redis.get(key);
        if (StringUtils.isBlank(counts)) {
            counts = "0";
        }
        return counts;
    }

}
